package com.study_online.aop;

import com.study_online.annotation.UpdateTimeAnnotation;
import com.study_online.mapper.TimeMapper;
import com.study_online.mapper.UserMapper;
import com.study_online.utils.AnnotationResolver;
import com.study_online.utils.TimeUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author 张天霸
 */

@Component
@Aspect
public class TimeAspect {


    @Autowired
    private TimeMapper timeMapper;


    @Pointcut("@annotation(com.study_online.annotation.UpdateTimeAnnotation)")
    public void update(){

    }

    @AfterReturning("update()")
    public void updateTime(JoinPoint joinPoint){
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        UpdateTimeAnnotation updateTimeAnnotation = method.getAnnotation(UpdateTimeAnnotation.class);
        String str = updateTimeAnnotation.Id();
        Integer Id = (Integer)AnnotationResolver.newInstance().resolver(joinPoint, str);
        String idName = updateTimeAnnotation.IdName();
        String tablename = updateTimeAnnotation.tablename();
        String time = TimeUtils.getTime();
        timeMapper.updateTime(time,tablename,Id,idName);
    }
}